package com.sugon.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.RandomUtil;
import com.sugon.dao.ModuleStatusChangeRecordDao;
import com.sugon.entity.ModuleStatusChangeRecordEntity;
import com.sugon.modules.mold.consts.MoldStatusChangeAction;
import com.sugon.service.ModuleStatusChangeRecordService;
import com.sugon.utils.Constant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * 模具状态变化记录Service实现类
 *
 * @author YuXD
 * @date 2021-04-05 09:21:10
 */
@Service("moduleStatusChangeRecordService")
public class ModuleStatusChangeRecordServiceImpl implements ModuleStatusChangeRecordService {

    @Autowired
    private ModuleStatusChangeRecordDao moduleStatusChangeRecordDao;

    @Override
    public boolean checkMoldStatusChangable(String moldId, MoldStatusChangeAction changeAction) {
        ModuleStatusChangeRecordEntity record = moduleStatusChangeRecordDao.getLastestStatusChangeRecordByMoldId(moldId);
        return record == null || changeAction.name().equals(record.getAction());
    }

    @Override
    public ModuleStatusChangeRecordEntity queryObject(String id) {
        return moduleStatusChangeRecordDao.queryObject(id);
    }

    @Override
    public List<ModuleStatusChangeRecordEntity> queryList(Map<String, Object> map) {
        return moduleStatusChangeRecordDao.queryList(map);
    }

    @Override
    public int queryTotal(Map<String, Object> map) {
        return moduleStatusChangeRecordDao.queryTotal(map);
    }

    @Override
    public int save(ModuleStatusChangeRecordEntity moduleStatusChangeRecord) {
        moduleStatusChangeRecord.setId(RandomUtil.randomString(32));
        moduleStatusChangeRecord.setGmtCreate(DateUtil.date());
        moduleStatusChangeRecord.setGmtModified(moduleStatusChangeRecord.getGmtCreate());
        moduleStatusChangeRecord.setDelFlag(Constant.IS_NOT_DEL_INT);
        return moduleStatusChangeRecordDao.save(moduleStatusChangeRecord);
    }

    @Override
    public int update(ModuleStatusChangeRecordEntity moduleStatusChangeRecord) {
        moduleStatusChangeRecord.setGmtModified(DateUtil.date());
        return moduleStatusChangeRecordDao.update(moduleStatusChangeRecord);
    }

    @Override
    public int delete(String id) {
        return moduleStatusChangeRecordDao.delete(id);
    }

    @Override
    public int deleteBatch(String[] ids) {
        return moduleStatusChangeRecordDao.deleteBatch(ids);
    }

}
